import { Component ,OnInit} from '@angular/core';
import { appService } from '../my-service/app.service';
import { fadeIn } from '../animate';
import { classService } from '../my-service/class.service';
import { UtilService } from '../my-service/util.service'
import { Router, NavigationEnd, ActivatedRoute ,Params} from '@angular/router';
@Component({
  selector: 'canvaslist-root',
  templateUrl: './canvaslist.component.html',
  styleUrls: ['./canvaslist.component.css'],
  animations: [fadeIn]
})
export class canvaslistComponent implements OnInit {
  constructor(private appservice: appService,
    private router: Router,private utilService: UtilService,
    private activatedRoute: ActivatedRoute,) { }
    breadcrumb = {
      name: '搜索训练',
      path: '/home/list',
      children: {
        name: '加载中。。。'
      }
    }
    trainmsg={
      CreateIP:'',
      CreateTime:'',
      CreaterName:'',
      CreaterOrgName:'',
      Description:'',
      FK_CanvasGuid:'',
      FK_ClassGuid:'',
      FK_CreatorGuid:'',
      ID:'',
      PK_TrainningGuid:'',
      Password:'',
      Status:'',
      Title:'',
      TrainNumber:'',
      Code:''
    }

    FK_TrainningGuid= this.activatedRoute.snapshot.paramMap.get('FK_TrainningGuid');;
    canvaslist=[]
    nzTotal;
    nzPageIndex=1;
    smcanvaslist=[];
    nzPageSize=9;
    load=false;
    addwindow=false;
    form={
      Name:'',
      Password:'0000'
    }
    passwordwindowdata={
      Backcolor:'',
      ChartsData:'',
      CreateIP:'',
      CreateTime:'',
      FK_TrainningGuid:'',
      ID:'',
      Name:'',
      Num:'',
      PK_CanvasGroupGuid:'',
      PassWord:'',
      Status:''
    }
    password=''
    passwordwindow=false;
    ngOnInit() {
      this.shuiji()
      this.appservice.trainmsg(this.FK_TrainningGuid).then(re=>{
        this.trainmsg=re
        this.breadcrumb.children.name=re.Title
        console.log(this.trainmsg)
      })
      this.viewload()
    }
    viewload(){
      this.smcanvaslist=[]
      this.appservice.getcanvasgroup(this.FK_TrainningGuid,0,10000).then(re2=>{
        this.canvaslist=this.ColorAndNum(re2);
        this.nzTotal=re2.length;
        let len:number;
        if( this.nzTotal>this.nzPageSize) len=this.nzPageSize
        else if (this.nzTotal<=this.nzPageSize) len=this.nzTotal
        for(let i=0;i<len;i++){
          this.smcanvaslist.push(re2[i])
          
        }
      }).then(()=>{
        this.load=true;
      })
    }
    initialize(){
      this.form={
        Name: ``,
        Password: this.getCode(4)
      }
    }
    initialize2(){
      this.password=''
      this.passwordwindowdata={
        Backcolor:'',
        ChartsData:'',
        CreateIP:'',
        CreateTime:'',
        FK_TrainningGuid:'',
        ID:'',
        Name:'',
        Num:'',
        PK_CanvasGroupGuid:'',
        PassWord:'',
        Status:''
      }
    }
     //获取canvasgroup的color和有多少个标签 
  ColorAndNum(canvasgroup) {
    canvasgroup.forEach(element => {
      this.appservice.getNoteNum(element.PK_CanvasGroupGuid).then(re2 => {
        element.Num = re2;
        if (re2 >= 0 && re2 < 40) { element.Backcolor = "#FC9399" }
        else if (re2 >= 40 && re2 < 60) { element.Backcolor = "#E56599" }
        else { element.Backcolor = "#CD3699" }
      })
    })
    return canvasgroup
  }
  shuiji() {
    this.form.Password = this.getCode(4);
  }
  getCode(num) {
    let id = "";
    for (let i = 1; i <= num; i++) {
      let n = Math.floor(Math.random() * 10.0).toString(10);
      id += n;

    }
    return id;
  }
    pagin(){
      this.smcanvaslist = []
      let n = (this.nzPageIndex-1) * this.nzPageSize;
      for (let i = 0; i < this.nzPageSize; i++) {
        if ((n + i) < this.canvaslist.length) {
          this.smcanvaslist.push(this.canvaslist[i + n])
        }
      }
    }
    checknum(num, item) {
      if (item == 'Password') {
        if (JSON.stringify(this.form[item]).length > num) {
          this.form[item] = parseInt(JSON.stringify(this.form[item]).slice(0, num))
        }
      } else {
        if (this.form[item].length > num) {
          this.form[item] = this.form[item].slice(0, num)
        }
      }
    }
    getDate() {
      let date = new Date(); //取当前时间
      let month = date.getMonth() + 1;
      let months, strDates;
      let strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        months = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDates = "0" + strDate;
      } else {
        strDates = strDate
      }
      let currentdate =   months + strDates;
      return currentdate;
    }
    add(){
      if (this.form.Name == '') {
        this.utilService.remind('error', '名字不能为空!')
        return
      }
      if (this.form.Password.length != 4) {
        this.utilService.remind('error', '请填写四位密码!')
        return
      }
      this.appservice.postNewCanvasGroup(this.form,this.FK_TrainningGuid).then((re) => {
        if (re.status == "success") {
          this.viewload();
          this.initialize();
          this.addwindow=false;
        }
      })
    }
    goto(index){
      // '
      this.passwordwindow=true;
      this.passwordwindowdata=this.smcanvaslist[index]
      // 
    }
    join(){
      if(this.password=='168'){
        this.router.navigate([`/canvas/start/${this.passwordwindowdata.PK_CanvasGroupGuid}`])
      }
      else{
        if(this.password==this.passwordwindowdata.PassWord){
          this.router.navigate([`/canvas/start/${this.passwordwindowdata.PK_CanvasGroupGuid}`])
        }else{
          this.utilService.remind('error','密码错误')
        }
      }
    }
}
